package br.com.fiap.mercadinho.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.com.fiap.mercadinho.bean.Customer;
import br.com.fiap.mercadinho.common.DAOException;
import br.com.fiap.mercadinho.common.RequestKey;
import br.com.fiap.mercadinho.common.ServiceException;
import br.com.fiap.mercadinho.dao.CustomerDAO;

/**
 * Servlet implementation class CustomerServlet
 */
@WebServlet("/customer")
public class CustomerServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public CustomerServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		CustomerDAO dao = new CustomerDAO();

		try {
			List<Customer> customers = dao.findAllCustomer();
			request.setAttribute("customerList", customers);
			sendResponse(request, response);

		} catch (DAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		CustomerDAO dao = new CustomerDAO();

		try {

			String action = request.getParameter("action");
			request.setAttribute("action", action);

			// atualiza lista de clientes na tela.
			request.setAttribute("customerList", dao.findAllCustomer());

			if (action.equals("show")) {
				List<Customer> customers = dao.findAllCustomer();
				request.setAttribute("customerList", customers);
			} else if (action.equals("add")) {
				Customer cust = getCustomerFromRequest(request);
				if (!isValidCustomer(request, cust)) {
					request.setAttribute("customer",
							getCustomerFromRequest(request));
					sendResponse(request, response);
					return;
				} else {
					dao.saveCustomer(cust);
					List<Customer> customers = dao.findAllCustomer();
					request.setAttribute("customerList", customers);
				}
			} else if (action.equals("select")) {
				Integer idCustomer = Integer.parseInt(request
						.getParameter("customerIdSelected"));
				Customer customer = dao.findCustomerById(idCustomer);
				request.setAttribute("customer", customer);
				List<Customer> customers = dao.findAllCustomer();
				request.setAttribute("customerList", customers);
			} else if (action.equals("update")) {
				Customer cust = getCustomerFromRequest(request);
				if (!isValidCustomer(request, cust)) {
					request.setAttribute("customer",
							getCustomerFromRequest(request));
					sendResponse(request, response);
					return;
				} else {
					dao.updateCustomer(cust);
					List<Customer> customers = dao.findAllCustomer();
					request.setAttribute("action", "show");
					request.setAttribute("customerList", customers);
				}
			}
			sendResponse(request, response);
		} catch (DAOException e) {
			request.setAttribute(RequestKey.INFO_MESSAGE_KEY.getValue(),
					e.getMessage());
			sendResponse(request, response);
		}
	}

	private void sendResponse(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.getRequestDispatcher("customer.jsp").forward(request, response);
	}

	private boolean isValidCustomer(HttpServletRequest request, Customer cust)
			throws ServiceException {
		String resultValidate = cust.validateCustomer();

		if (!resultValidate.equals("")) {
			request.setAttribute(RequestKey.INFO_MESSAGE_KEY.getValue(),
					resultValidate);
			return false;
		}
		return true;
	}

	private Customer getCustomerFromRequest(HttpServletRequest request) {
		Customer cust = new Customer();
		cust.setId(request.getParameter("customerIdSelected"));
		cust.setName(request.getParameter("name"));
		cust.setCpfNumber(request.getParameter("cpfNumber"));
		cust.setBirthDate(request.getParameter("birthDate"));
		return cust;
	}

}
